package com.heima.article.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.heima.article.entity.ApArticle;
import com.heima.article.entity.ApAuthor;
import com.heima.article.mapper.ApAuthorMapper;
import com.heima.article.service.ApAuthorService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.article.dtos.AuthorDto;
import com.heima.model.media.dtos.WmNewsResultDTO;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 * APP文章作者信息表 服务实现类
 * </p>
 *
 * @author HM
 * @since 2023-02-01
 */
@Service
public class ApAuthorServiceImpl extends ServiceImpl<ApAuthorMapper, ApAuthor> implements ApAuthorService {
    /**
     *  审核通过 创建文章的作者
     * @param dto
     * @return
     */
    @Override
    public Integer createAuthor(AuthorDto dto) {
        LambdaQueryWrapper<ApAuthor> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ApAuthor::getUserId,dto.getUserId());
        ApAuthor apAuthor = this.getOne(lqw);
        //没有数据  新增  否则更新
        if (apAuthor == null){
            ApAuthor author = buildAuthor(dto, null);
            this.save(author);
            return author.getId();
        }
        ApAuthor author = buildAuthor(dto, apAuthor.getId());
        this.updateById(author);
        return author.getId();
    }

    @Override
    public ApAuthor findAuthor(Integer wmuserid) {
        LambdaQueryWrapper<ApAuthor> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ApAuthor::getWmUserId,wmuserid);
        ApAuthor author = this.getOne(lqw);
        return author;

    }


    private ApAuthor buildAuthor(AuthorDto dto,Integer authorId) {
        ApAuthor author = new ApAuthor();
        author.setId(authorId);
        author.setName(dto.getName());
        author.setType(dto.getType());
        if (authorId == null){
            author.setUserId(dto.getUserId());
            author.setWmUserId(dto.getWmUserId());
            author.setCreatedTime(new Date());
        }
        return author;
    }






}
